package com.xy.etl.ibatis.core;

import com.xy.etl.ibatis.node.XySQLSuperNode;
import com.xy.etl.ibatis.node.impl.XySQLMapperNode;
import ognl.DefaultClassResolver;
import ognl.DefaultTypeConverter;
import ognl.Ognl;
import ognl.OgnlContext;

import java.util.Map;

/**
 * 执行上下文
 */
public class XySQLTaskContext {

    //Mapper
    private XySQLMapperNode mapper;

    //公式执行上下文
    private OgnlContext context;
    //参数
    private Map<String, Object> param;

    /**
     * 初始化
     *
     * @param mapper
     */
    public XySQLTaskContext(XySQLMapperNode mapper) {
        this.mapper = mapper;
    }

    /**
     * 初始化公式
     */
    public void initPredicate(Map<String, Object> param) {
        this.param = param;
        this.context = (OgnlContext) Ognl.createDefaultContext(this, new XySQLMemberAccess(true), new DefaultClassResolver(), new DefaultTypeConverter());
        if (param != null) {
            context.setValues(param);
        }
    }


    /**
     * 设定参数
     *
     * @param key
     * @param value
     */
    public void putPara(String key, Object value) {
        context.put(key, value);
        param.put(key,value);
    }

    /**
     * 设定参数
     *
     * @param
     */
    public void removePara(String key) {
        context.remove(key);
        param.remove(key);
    }

    /**
     * 取得Mapper
     *
     * @return
     */
    public XySQLMapperNode getMapper() {
        return mapper;
    }

    /**
     * 取得指定节点
     *
     * @param id
     * @return
     */
    public XySQLSuperNode getSqlFragmentById(String id) {
        return mapper.getSqlSegmentById(id);
    }

    /**
     * 取得模型信息
     *
     * @return
     */
    public Map<String, Map<String, String>> getItemMap() {
        return mapper.getItemMap();
    }

    /**
     * 表达式上下文
     *
     * @return
     */
    public OgnlContext getConditionContext() {
        return context;
    }

    /**
     * 取得参数
     *
     * @return
     */
    public Map<String, Object> getParam() {
        return param;
    }
}
